﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceProcess;

namespace OpenDynDnsChanger.AdminApp.Controls
{
    /// <summary>
    /// Interaktionslogik für ServiceStatusControl.xaml
    /// </summary>
    public partial class ServiceStatusControl : UserControl
    {
        public ServiceStatusControl()
        {
            InitializeComponent();

            App.Current.ServiceChanged += new RoutedEventHandler(Current_ServiceChanged);
        }

        ~ServiceStatusControl()
        {
            try
            {
                if (App.Current != null)
                    App.Current.ServiceChanged -= Current_ServiceChanged;
            }
            catch
            {

            }
        }

        void Current_ServiceChanged(object sender, RoutedEventArgs e)
        {
            UpdateControllerStatus();
        }

        #region Controller

        public ServiceController Controller
        {
            get { return (ServiceController)GetValue(ControllerProperty); }
            set { SetValue(ControllerProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Controller.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ControllerProperty =
            DependencyProperty.Register("Controller", typeof(ServiceController), typeof(ServiceStatusControl), new UIPropertyMetadata(null, OnControllerChanged));

        private static void OnControllerChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ServiceStatusControl).UpdateControllerStatus();
        } 
        #endregion

        void UpdateControllerStatus()
        {
            try
            {
                this.tb_StatusText.Text = Controller.Status.ToString();
            }
            catch { }
        }


    }
}
